package com.zxinlee.m1;

import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;

import java.util.Scanner;

/**
 * 同步消息/延时消息
 */
public class Producer {
    public static void main(String[] args) throws MQClientException, MQBrokerException, RemotingException, InterruptedException {
        //创建生产者
        DefaultMQProducer producer01 = new DefaultMQProducer("producer01");

        //设置 name server
        producer01.setNamesrvAddr("192.168.64.141:9876");

        //启动生产者,连接服务器
        producer01.start();

        /**
         * Topic01 -- 相当于一级分类
         * Tag01   -- 相当于二级分类(可选)
         */
        //消息封装成Message对象
        while (true) {
            System.out.println("输入消息 :");
            String msg = new Scanner(System.in).nextLine();
            //封装
            //import org.apache.rocketmq.common.message.Message;
            Message message = new Message("Topic01", "Tag01", msg.getBytes());
            //发送消息 -- 延时消息
            //消息发送到 Rocketmq 服务器后， 延迟一定时间再向消费者进行投递。
            message.setDelayTimeLevel(2);//只能设置延时级别,共十八个级别
            SendResult result = producer01.send(message);

            System.out.println(result);
            //queueId=2         队列编号
            //queueOffset=0     队列内 消息的序号
            //SendResult [sendStatus=SEND_OK, msgId=B0D41B281B4018B4AAC287F7ACF40000, offsetMsgId=C0A8408D00002A9F000000000002BEB2, messageQueue=MessageQueue
            //                  [topic=Topic01, brokerName=localhost.localdomain, queueId=2], queueOffset=0]
        }
    }
}
